﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CdcSoftware.Pivotal.Applications.Core.Client;
using System.Data;
using CdcSoftware.Pivotal.Applications.Core.Common;
using CdcSoftware.Pivotal.Engine;
using CdcSoftware.Pivotal.Engine.Client.Services.Interfaces;
using Pcm.Retail.Client;
using System.Windows.Forms;
using CdcSoftware.Pivotal.Engine.UI.Forms;
using CdcSoftware.Pivotal.Engine.UI.DataTypes.FormParams;

namespace Pcm.Retail.Client.FormTasks
{
    public partial class RE_POS_Member : FormClientTask
    {
        /// <summary>
        /// Replace the old membership card with new one.
        /// </summary>
        /// <param name="sender">The control associated with the event triggered</param>
        /// <param name="args">The argument for the event</param>
        /// <history>
        /// #Revision   Date            Author     Description
        /// 6.0.10.1    2012-08-12      AA         Initial version for Retail.
        /// </history>
        public virtual void ReplaceMembershipCard_Click(PivotalControl sender, EventArgs args)
        {
            //Implement logic
            string title = "Replace Membership Card";

            IInputDialogService inputDialogService = ClientContext.ClientAPIService.GetService<IInputDialogService>();
            InputParamsCollection paramCollection = new InputParamsCollection();
            InputParam paramMembershipId = new InputParam();
            InputDialogStyle dialogStyle = new InputDialogStyle();
            dialogStyle.BoldLabels = true;

            paramMembershipId.Label = "New Membership Id: ";
            paramMembershipId.Type = InputParamType.String;
            paramCollection.Add(paramMembershipId);
            DialogResult dialogResult = inputDialogService.ShowDialog(paramCollection, dialogStyle, title);

            if (dialogResult != DialogResult.Cancel)
            {
                if (!string.IsNullOrWhiteSpace(TypeConvert.ToString(paramMembershipId.Value)))
                {
                    if (TypeConvert.ToString(this.PrimaryDataRow["RE_Membership_ID"]).ToLower() != TypeConvert.ToString(paramMembershipId.Value).ToLower())
                    {
                        //Run a dup check for the new Membership Id.
                        DataTable contactsForMembership = Globals.GetDataTable("RE_Members with with Brand Id? Membership ID? Contact Id Not?",
                                new object[] { this.PrimaryDataRow["RE_Brand_Id"], paramMembershipId.Value, this.RecordId }, new string[] { "Contact_Id" });

                        if (contactsForMembership != null && contactsForMembership.Rows.Count > 0)
                        {
                            //Notify for the duplicate existence.
                            PivotalMessageBox.Show("There is already a Member with the Membership Id.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            ReplaceMembershipCard_Click(sender, args);
                        }
                        else
                        {
                            this.PrimaryDataRow["RE_Membership_ID"] = paramMembershipId.Value;
                            //Add secondary
                            string segmentName = "RE_Membership_History_Contact_";
                            this.FormView.NewSecondaryRecord(segmentName);
                            DataTable dtSecondary = this.DataSet.Tables[segmentName];
                            DataRow drNewRow = dtSecondary.Rows[dtSecondary.Rows.Count - 1];
                            drNewRow["Activity"] = "Card Replaced";
                            drNewRow["Activity_Date"] = DateTime.Now;
                            drNewRow["Activity_Description"] = "Old membership Card: " + TypeConvert.ToString(this.PrimaryDataRow["RE_Membership_ID", DataRowVersion.Original]) +
                                " has been replaced with new membership card: " + this.PrimaryDataRow["RE_Membership_ID"];
                        }
                    }
                    else
                    {
                        PivotalMessageBox.Show("Old and New Membership Id's are the same.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //MessageBox.Show("Old membership card and new membership card number are the same.");
                    }
                }
                else
                    ReplaceMembershipCard_Click(sender, args);
            }
        }
    }
}
